<?php
	require_once 'functions.php';
	
	function uploadQuote($param){
		for($i = 0; $i < count($param); $i += 3){
			$first_name = mysql_real_escape_string($param[$i]);
			$name 		= mysql_real_escape_string($param[$i+1]);
			$quote 		= mysql_real_escape_string($param[$i+2]);
			
			if($name && $quote){
				if(!query("SELECT * FROM author WHERE first_name = '$first_name' AND name = '$name'", true)){
					query("INSERT INTO author(first_name, name) VALUES('$first_name','$name')");	
				}
				if(!query("SELECT * FROM quote WHERE quote = '$quote'", true)){
					query("INSERT INTO quote(author_id, quote) SELECT id, '$quote' FROM author WHERE first_name = '$first_name' AND name = '$name'");		
				}
					print 'Upload successful!<br>';
			}else{
				print 'Upload failed!<br>';
			}
		}
	}
	
	function uploadQuoteFile(){
		if($_FILES['quoteFile']['tmp_name']){
			$file = file_get_contents($_FILES['quoteFile']['tmp_name']);
			$file = str_replace("\r", "", $file);
			$file = str_replace("\n", "", $file);
			
			$param = explode("|", $file);
			
			uploadQuote($param);
		}else{
			print 'File upload failed!';	
		}
	}
	
	if(isset($_GET['fn'])){
		$fn = $_GET['fn'];   
		
		if(isset($_GET['param'])){
			$paramArray = explode("|",$_GET['param']);
			$fn($paramArray);
		}else{
			$fn();
		}  
	} 
	

?>